<?php

/* * *
 * **公共验证控制器CommonAction
 * **
 * * */

class CommonAction extends BaseAction {
    protected $dtpass = false;


    public function __construct() {
        $dtoken = I('dtoken');
        if ($dtoken && $dtoken=='brs2015gogogo') {//调试token
            $this->dtpass = true;
        }
        else {
            parent::__construct();
            $session_uid = $this->uid;
            if (empty($session_uid)) {
                $auth = cookie('auth'); //读取"记住我"cookie
                $now = time();
                list($email, $identifier, $token) = explode(':', $auth);
                if (!($email && ctype_alnum($identifier) && ctype_alnum($token))) {
                    $this->redirect('Login/index@'.getDomain());
                }
                $arr = M('user')->where(array('email' => array('eq', $email)))->find();
                if ($arr) {
                    $lidentifier = md5(G_SALT . md5($email . G_SALT));
                    if ($token != $arr['token'] || $now > $arr['timeout'] || $identifier != $lidentifier) {
                        $this->redirect('Login/index@'.getDomain());
                    }
                    $session_uid = $arr['id'];
                }
            }
            else {
                $resU = M('user')->where('id='.$session_uid)->find();
                if ($resU['identifer']!='logined') {
                    $this->redirect('Login/index@'.getDomain());
                }
            }
        }
        Log::write("[CommonAction::__construct]uid:".$this->uid, Log::INFO);
    }

    //ajax失败返回
    protected function ajaxf($msg='') {
        echo json_encode(array('success'=>0,'msg'=>$msg));
        exit;
    }
    
    //ajax成功返回
    protected function ajaxs($code=1,$msg='') {
        echo json_encode(array('success'=>$code,'msg'=>$msg));
        exit;
    }

    //邮件发送测试
    public function sendmail() {
        $resMq = M('mailqueue')->where("receiver='t2@foxmail.com'")->find();
        $ret = think_send_mail('edianlab@foxmail.com', 'lvwker', $resMq['subject'], $resMq['body']);
        if(!$ret){
            echo 'failed';   
        }
    }
    
    //目标测试
    public function tpltest() {
        $this->display();
    }
    
}
?>